<?php

class Table {

    private $ID;
    private $name;
    private $source;
    private $physicalName;
    private $contents;
    private $length;
    private $description;

    public function __construct() {
        $num = func_num_args();
        $args = func_get_args();
        switch ($num) {
            case 0:
                $this->__construct0();
                break;
            case 1:
                $this->__construct1($args[0]);
                break;
            default:
                throw new Exception('calling wrong constructor');
        }
    }

    private function __construct0() {
        NULL;
    }

    private function __construct1($tableID) {
        $query = new Query();
        $query->select("*");
        $query->where("ID", $tableID);
        $query->table("tables");
        $query->build('SELECT');
        $query->exec();
        $record = $query->fetchRow();
        $this->ID = $record['ID'];
        $this->name = $record['name'];
        $this->source = $record['source'];
        $this->physicalName = $record['physicalName'];
        $this->contents = $record['contents'];
        $this->length = $record['length'];
        $this->description = $record['description'];
    }

    public function convertToArray() {
        return get_object_vars($this);
    }

    public function __toString() {
        echo '<pre>';
        print_r($this);
        echo '</pre>';
    }

    public function getID() {
        return $this->ID;
    }

    public function setID($ID) {
        $this->ID = $ID;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getSource() {
        return $this->source;
    }

    public function setSource($source) {
        $this->source = $source;
    }

    public function getPhysicalName() {
        return $this->physicalName;
    }

    public function setPhysicalName($physicalName) {
        $this->physicalName = $physicalName;
    }

    public function getContents() {
        return $this->contents;
    }

    public function setContents($contents) {
        $this->contents = $contents;
    }

    public function getLength() {
        return $this->length;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function add() {
        $query = new Query();
        $query->val("name", $this->name);
        $query->val("source", $this->source);
        $query->val("physicalName", $this->physicalName);
        $query->val("contents", $this->contents);
        $query->val("length", $this->length);
        $query->val("description", $this->description);
        $query->table("tables");
        $query->build('INSERT');
        $query->exec();

        $this->ID = $query->getLastInsertID();
    }

    public function update() {

        $query = new Query();
        $query->val("name", $this->name);
        $query->val("source", $this->source);
       $query->val("physicalName", $this->physicalName);
        $query->val("contents", $this->contents);
        $query->val("length", $this->length);
        $query->val("description", $this->description);
        $query->where("ID", $this->ID);
        $query->table("tables");
        $query->build('UPDATE');
        $query->exec();
    }

    public function delete() {
        $fieldIDs = TableManager::getInstance()->getTableFields($this->ID);
        foreach($fieldIDs as $fieldID){
            $field = new Field($fieldID);
            $field->delete();
        }
        
        $query = new Query();
        $query->where('ID', $this->ID);
        $query->table("tables");
        $query->exec('DELETE');
    }

}

?>
